home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / BitTorrent / selectpoll.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  2.2 KB  |  107 lines

  1. # Written by Bram Cohen
  2. # see LICENSE.txt for license information
  3.  
  4. from select import select, error
  5. from time import sleep
  6. from types import IntType
  7. from bisect import bisect
  8. POLLIN = 1
  9. POLLOUT = 2
  10. POLLERR = 8
  11. POLLHUP = 16
  12.  
  13. class poll:
  14.     def __init__(self):
  15.         self.rlist = []
  16.         self.wlist = []
  17.         
  18.     def register(self, f, t):
  19.         if type(f) != IntType:
  20.             f = f.fileno()
  21.         if (t & POLLIN) != 0:
  22.             insert(self.rlist, f)
  23.         else:
  24.             remove(self.rlist, f)
  25.         if (t & POLLOUT) != 0:
  26.             insert(self.wlist, f)
  27.         else:
  28.             remove(self.wlist, f)
  29.         
  30.     def unregister(self, f):
  31.         if type(f) != IntType:
  32.             f = f.fileno()
  33.         remove(self.rlist, f)
  34.         remove(self.wlist, f)
  35.  
  36.     def poll(self, timeout = None):
  37.         if self.rlist != [] or self.wlist != []:
  38.             r, w, e = select(self.rlist, self.wlist, [], timeout)
  39.         else:
  40.             sleep(timeout)
  41.             return []
  42.         result = []
  43.         for s in r:
  44.             result.append((s, POLLIN))
  45.         for s in w:
  46.             result.append((s, POLLOUT))
  47.         return result
  48.  
  49. def remove(list, item):
  50.     i = bisect(list, item)
  51.     if i > 0 and list[i-1] == item:
  52.         del list[i-1]
  53.  
  54. def insert(list, item):
  55.     i = bisect(list, item)
  56.     if i == 0 or list[i-1] != item:
  57.         list.insert(i, item)
  58.  
  59. def test_remove():
  60.     x = [2, 4, 6]
  61.     remove(x, 2)
  62.     assert x == [4, 6]
  63.     x = [2, 4, 6]
  64.     remove(x, 4)
  65.     assert x == [2, 6]
  66.     x = [2, 4, 6]
  67.     remove(x, 6)
  68.     assert x == [2, 4]
  69.     x = [2, 4, 6]
  70.     remove(x, 5)
  71.     assert x == [2, 4, 6]
  72.     x = [2, 4, 6]
  73.     remove(x, 1)
  74.     assert x == [2, 4, 6]
  75.     x = [2, 4, 6]
  76.     remove(x, 7)
  77.     assert x == [2, 4, 6]
  78.     x = [2, 4, 6]
  79.     remove(x, 5)
  80.     assert x == [2, 4, 6]
  81.     x = []
  82.     remove(x, 3)
  83.     assert x == []
  84.  
  85. def test_insert():
  86.     x = [2, 4]
  87.     insert(x, 1)
  88.     assert x == [1, 2, 4]
  89.     x = [2, 4]
  90.     insert(x, 3)
  91.     assert x == [2, 3, 4]
  92.     x = [2, 4]
  93.     insert(x, 5)
  94.     assert x == [2, 4, 5]
  95.     x = [2, 4]
  96.     insert(x, 2)
  97.     assert x == [2, 4]
  98.     x = [2, 4]
  99.     insert(x, 4)
  100.     assert x == [2, 4]
  101.     x = [2, 3, 4]
  102.     insert(x, 3)
  103.     assert x == [2, 3, 4]
  104.     x = []
  105.     insert(x, 3)
  106.     assert x == [3]
  107.